<?php
/**
 * Klasse zum Aufbau und Validierung von Formularen
 *
 * @author		Joachim Ruf
 * @copyright	Copyright (c) 2003-2013 Loresoft Software (http://www.loresoft.de)
 * @license		http://yapary.loresoft.de/lizenzbedingungen     New BSD License
 *
 * */


Class Yapary_Form_Formular_List
{
	/* @var $arrElement Yapary_Form_Formular_Element[] */
	private $arrElement		= array();


	/**
	 * */
	function __construct () {
	}


	/**
	 * Liefert eine Zeile zurueck.
	 * @param int $i	: Auszugebende Zeile
	 * @return Yapary_Form_Formular_Element
	 * */
	public function addElement ($_element) {
		return $this->arrElement[] = new Yapary_Form_Formular_Element($_element);
	}


	/**
	 * @desc Liefert alle Zeilen zurueck.
	 * @return Yapary_Form_Formular_Element[]
	 * */
	public function getList () {
		return $this->arrElement;
	}



	/**
	 * @desc Liefert eine Liste aller Elementnamen zurueck
	 * @return arrays
	 * */
	public function getListNameValue() {
		$arr = array();

		/* @var $oElement Yapary_Form_Formular_Element */
		foreach ($this->arrElement as $key => $oElement) {
			$arr[$oElement->getName()] = $oElement->getValue();
		}

		return $arr;
	}


	/**
	 * @desc Gibt an, ob das Formular bereits versendet wurde.
	 * @return bool
	 * */
	public function hasSend () {
		$valid = true;

		if (count($this->arrElement)) {
			/* @var $oElement Yapary_Form_Formular_Element */
			foreach ($this->arrElement as $key => $oElement) {
				if ($oElement->isRequired() === true && $oElement->hasSend() === false) {
					$valid = false;
					//echo $oElement->getName(); // Nichtgesendetes Element ausgeben
				}
			}
		}

		return $valid;
	}


	/**
	 * @desc Definiert, dass vorhandene Daten in den FOrmularfeldern eingetragen bleiben sollen.
	 * @return bool
	 * */
	public function keepValidData () {
		if (count($this->arrElement)) {
			/* @var $oElement Yapary_Form_Formular_Element */
			foreach ($this->arrElement as $key => $oElement) {
				$oElement->validate();
			}
		}
	}


	/**
	 * @desc Definiert, dass vorhandene Daten in den FOrmularfeldern eingetragen bleiben sollen.
	 * @return bool
	 * */
	public function useSessionLookup () {
		if (count($this->arrElement)) {
			/* @var $oElement Yapary_Form_Formular_Element */
			foreach ($this->arrElement as $key => $oElement) {
				$oElement->useSessionLookup();
			}
		}
	}


	/**
	 * @desc Gibt an, ob das Formular bereits versendet wurde.
	 * @return bool
	 * */
	public function hasError () {
		if (count($this->arrElement)) {
			/* @var $oElement Yapary_Form_Formular_Element */
			foreach ($this->arrElement as $key => $oElement) {
				if ($oElement->hasError() === true) {
					return true;
				}
			}
		}

		return false;
	}


	/**
	 * @desc Liefert alle entstandenen Fehler.
	 * @return array
	 * */
	public function getErrorList () {
		$arrError = array();

		if (count($this->arrElement)) {
			/* @var $oElement Yapary_Form_Formular_Element */
			foreach ($this->arrElement as $key => $oElement) {
				if ($oElement->getError() !== NULL) {
					$arrError[] = $oElement->getError();
				}
			}
		}

		return $arrError;
	}


	/**
	 * @desc Liefert alle entstandenen Fehler.
	 * @return array
	 * */
	public function getErrorMessages () {
		$result = NULL;

		if (count($this->arrElement)) {
			/* @var $oElement Yapary_Form_Formular_Element */
			foreach ($this->arrElement as $key => $oElement) {
				if ($oElement->getError() !== NULL) {
					$result .= '<li>'.$oElement->getError().'</li>';
				}
			}

			$result = '<ul>'.$result.'</ul>';
		}

		return $result;
	}


	/**
	 * @desc Liefert die Anzahl an internen und externen Fehlern.
	 * @return int
	 * */
	public function getErrorCount () {
		$count = 0;

		if (count($this->arrElement)) {
			/* @var $oElement Yapary_Form_Formular_Element */
			foreach ($this->arrElement as $key => $oElement) {
				if ($oElement->hasError()) {
					$count++;
				}
			}
		}

		return $count;
	}


	/**
	 * @desc Gibt an, dass Fehlercodes aus der Validierung angezeigt werden sollen
	 * @return void
	 * */
	public function enableErrorReporting ($_enable = true) {
		if (count($this->arrElement)) {
			/* @var $oElement Yapary_Form_Formular_Element */
			foreach ($this->arrElement as $key => $oElement) {
				$oElement->enableErrorReporting($_enable);
			}
		}
	}


	/**
	 * @desc Validiert alle Formulareingaben.
	 * 		MUSS VOR DER ERSTEN VERWENDUNG VON DISPLAY() AUFGERUFEN WERDEN
	 * @return int|NULL	: Validierung erfolgreich=true, sonst false
	 * */
	public function validate () {
		$valid = true;

		if (count($this->arrElement)) {
			/* @var $oElement Yapary_Form_Formular_Element */
			foreach ($this->arrElement as $key => $oElement) {
				if ($oElement->validate() === false) {
					//echo $oElement->getName();
					$valid = false;
				}
			}
		}

		return $valid;
	}


	/**
	 * @desc Fuegt dem Element elName einen externen Fehler hinzu.
	 * @return string $elName	: Elementname
	 * @return string $_error	: Fehlermeldung
	 * */
	public function setExternalError ($elName, $_error) {
		if (count($this->arrElement)) {
			/* @var $oElement Yapary_Form_Formular_Element */
			foreach ($this->arrElement as $key => $oElement) {
				if ($oElement->getName() == $elName) {
					$oElement->setExternalError($_error);
					return true;
				}
			}
		}

		return false;
	}


	/**
	 * @desc Gibt das Formular aus.
	 * @param string $_name	: Name entspricht dem Tag-Attribut <tag name="" />
	 * @return string
	 * */
	public function display($_name) {
		/* @var $oElement Yapary_Form_Formular_Element */
		foreach ($this->arrElement as $key => $oElement) {
			if ($oElement->getName() == $_name) {
				return $oElement->getElement();
			}
		}

		throw new Exception('Element '.$_name.' does not exists');
	}


	/**
	 * @desc Prueft, ob ein Formularelement name="abc" existiert
	 * @param string $_name	: Name entspricht dem Tag-Attribut <tag name="" />
	 * @return string
	 * */
	public function exists($_name) {
		/* @var $oElement Yapary_Form_Formular_Element */
		foreach ($this->arrElement as $key => $oElement) {
			if ($oElement->getName() == $_name) {
				return true;
			}
		}

		return false;;
	}


	/**
	 * @desc Gibt das Formular aus.
	 * @param string $_name	: Name entspricht dem Tag-Attribut <tag name="" />
	 * @return string
	 * */
	public function getValue($_name) {
		/* @var $oElement Yapary_Form_Formular_Element */
		foreach ($this->arrElement as $key => $oElement) {
			if ($oElement->getName() == $_name) {
				$val	= $oElement->getValue();

				if (is_string($val) && strlen($val) >= 1) {
					$val	= trim($val);
				}
				return $val;
			}
		}

		throw new Exception('Element '.$_name.' does not exists');
	}


	/**
	 * @desc Gibt das Formular aus.
	 * @param string $_name	: Name entspricht dem Tag-Attribut <tag name="" />
	 * @return string
	 * */
	public function setValue($_name, $_value) {
		/* @var $oElement Yapary_Form_Formular_Element */
		foreach ($this->arrElement as $key => $oElement) {
			if ($oElement->getName() == $_name) {

				return $oElement->setValue($_value);
			}
		}

		throw new Exception('Element '.$_name.' does not exists');
	}


}


?>